package com.ruoyi.web.controller.system;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Sup;
import com.ruoyi.system.service.ISupService;
import com.ruoyi.framework.web.base.BaseController;
import com.ruoyi.common.page.TableDataInfo;
import com.ruoyi.common.base.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 供应商 信息操作处理
 * 
 * @author ruoyi
 * @date 2019-02-20
 */
@Controller
@RequestMapping("/system/sup")
public class SupController extends BaseController
{
    private String prefix = "system/sup";
	
	@Autowired
	private ISupService supService;
	
	@RequiresPermissions("system:sup:view")
	@GetMapping()
	public String sup()
	{
	    return prefix + "/sup";
	}
	
	/**
	 * 查询供应商列表
	 */
	@RequiresPermissions("system:sup:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Sup sup)
	{
		startPage();
        List<Sup> list = supService.selectSupList(sup);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出供应商列表
	 */
	@RequiresPermissions("system:sup:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Sup sup)
    {
    	List<Sup> list = supService.selectSupList(sup);
        ExcelUtil<Sup> util = new ExcelUtil<Sup>(Sup.class);
        return util.exportExcel(list, "sup");
    }
	
	/**
	 * 新增供应商
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存供应商
	 */
	@RequiresPermissions("system:sup:add")
	@Log(title = "供应商", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Sup sup)
	{		
		return toAjax(supService.insertSup(sup));
	}

	/**
	 * 修改供应商
	 */
	@GetMapping("/edit/{code}")
	public String edit(@PathVariable("code") String code, ModelMap mmap)
	{
		Sup sup = supService.selectSupById(code);
		mmap.put("sup", sup);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存供应商
	 */
	@RequiresPermissions("system:sup:edit")
	@Log(title = "供应商", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Sup sup)
	{		
		return toAjax(supService.updateSup(sup));
	}
	
	/**
	 * 删除供应商
	 */
	@RequiresPermissions("system:sup:remove")
	@Log(title = "供应商", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(supService.deleteSupByIds(ids));
	}
	
}
